package com.gears_software.sxdc.dccx.ui.activity;


import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.event.LogoutDriverMemberEvent;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.gears_software.sxdc.dccx.ui.util.OttoBus;
import com.github.mzule.activityrouter.annotation.Router;
import com.github.mzule.activityrouter.router.Routers;
import com.orhanobut.logger.Logger;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/6/6.
 */
@EActivity(R.layout.activity_my)
@Router(value = "myinfo", isEActivity = true)
public class MyInfoActivity extends AppCompatActivity {
    @App
    DCCXApplication app;
    @Pref
    DCCXSp_ sp;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView txt_right_icon1;
    @ViewById
    TextView txt_qianbao;
    @ViewById
    TextView txt_xingcheng;
    @ViewById
    TextView txt_duijiang;
    @ViewById
    TextView txt_yaoqing;
    @ViewById
    TextView txt_address;
    @ViewById
    TextView txt_dengji;
    @ViewById
    TextView txt_message_center;
    @ViewById
    TextView txt_tiaokuan;
    @ViewById
    TextView txt_about;
    @ViewById
    TextView txt_fankui;
    @ViewById
    TextView txt_shiming;
    @ViewById
    TextView txt_chezhu;
    @ViewById
    TextView txt_cheliang;
    @ViewById
    TextView txt_right_icon2;
    @ViewById
    TextView txt_right_icon3;
    @ViewById
    TextView txt_right_icon4;
    @ViewById
    TextView txt_right_icon5;
    @ViewById
    TextView txt_right_icon6;
    @ViewById
    TextView txt_right_icon7;
    @ViewById
    TextView txt_right_icon8;
    @ViewById
    TextView txt_right_icon9;
    @ViewById
    TextView txt_right_icon10;
    @ViewById
    TextView txt_right_icon12;
    @ViewById
    TextView txt_right_icon13;
    @ViewById
    TextView txt_right_icon14;
    @ViewById
    TextView txt_right_icon15;
    @ViewById
    TextView txt_fenshu;
    @ViewById
    LinearLayout ly_avatar;
    @ViewById
    TextView txt_username;
    @ViewById
    SimpleDraweeView img_avatar;
    @ViewById
    RelativeLayout rl_youhui;
    @ViewById
    RelativeLayout rl_yaoqing;
    @Bean
    GearsErrorHandler errorHandler;
    @RestService
    IMemberFinder memberFinder;
    @Bean
    OttoBus bus;
    MaterialDialogProgress progress;
    MemberModel memberModel;


    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        memberModel = new MemberModel();
        memberFinder.setRestErrorHandler(errorHandler);
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getCurrentMember();

    }

    void initIconFont() {
        actionbar_title.setText("我的");
        back_icon.setVisibility(View.VISIBLE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
        right_icon.setVisibility(View.GONE);
        txt_right_icon1.setTypeface(app.iconfont);
        txt_qianbao.setTypeface(app.iconfont);
        txt_xingcheng.setTypeface(app.iconfont);
        txt_duijiang.setTypeface(app.iconfont);
        txt_yaoqing.setTypeface(app.iconfont);
        txt_address.setTypeface(app.iconfont);
        txt_dengji.setTypeface(app.iconfont);
        txt_tiaokuan.setTypeface(app.iconfont);
        txt_message_center.setTypeface(app.iconfont);
        txt_about.setTypeface(app.iconfont);
        txt_fankui.setTypeface(app.iconfont);
        txt_shiming.setTypeface(app.iconfont);
        txt_chezhu.setTypeface(app.iconfont);
        txt_cheliang.setTypeface(app.iconfont);
        txt_right_icon2.setTypeface(app.iconfont);
        txt_right_icon3.setTypeface(app.iconfont);
        txt_right_icon4.setTypeface(app.iconfont);
        txt_right_icon5.setTypeface(app.iconfont);
        txt_right_icon6.setTypeface(app.iconfont);
        txt_right_icon7.setTypeface(app.iconfont);
        txt_right_icon8.setTypeface(app.iconfont);
        txt_right_icon9.setTypeface(app.iconfont);
        txt_right_icon10.setTypeface(app.iconfont);
        txt_right_icon12.setTypeface(app.iconfont);
        txt_right_icon13.setTypeface(app.iconfont);
        txt_right_icon14.setTypeface(app.iconfont);
        txt_right_icon15.setTypeface(app.iconfont);
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    if (memberModel.getAvatar() != null && !memberModel.getAvatar().equals("")) {
                        Uri uri = Uri.parse(memberModel.getAvatar() + RequestUrl.avatar);
                        img_avatar.setImageURI(uri);
                    }
//                    if (memberModel.getFname() != null && !memberModel.getFname().equals("")) {
//                        txt_username.setText(memberModel.getFname());
//                    }
                    if (memberModel.getNickname() != null && !memberModel.getNickname().equals("")) {
                        txt_username.setText(memberModel.getNickname());
                    } else {
                        txt_username.setText(memberModel.getDriverphone());
                    }
                }
            }
        }
        if (progress != null) {
            progress.dismiss();
        }
    }

    @Click
    void rl_youhui() {
        Intent intent = new Intent(this,RedeemCodeActivity_.class);
        startActivity(intent);
    }

    @Click
    void rl_yaoqing() {
        TastyToast.makeText(this, "敬请期待", app.SHORT, app.WARNING);
    }

    @Click
    void re_wallet() {
//        Intent intent = new Intent(this, MyWalletActivity_.class);
//        startActivity(intent);
        Routers.open(this, "gears_dccx://wallet");

    }

    @Click
    void re_trip() {
        Intent intent = new Intent(this, MyTripActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_chezhu() {
        if (memberModel != null && memberModel.getCarpooldriverauditstate() != null) {
            switch (memberModel.getCarpooldriverauditstate()) {
                case "1":
                    goJiaShiZheng();
                    break;
                case "2":
                    JSZtongguo();
                    break;
                case "3":
                    goJiaShiZheng();
                    break;
                case "4":
                    JSZtongguo();
                    break;
            }
        }

    }

    private void goJiaShiZheng() {
        Intent intent = new Intent(this, JiaShiZhengActivity_.class);
        startActivity(intent);
    }

    private void JSZtongguo() {
        Intent intent = new Intent(this, CZRZTGActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_cars() {
        Intent intent = new Intent(this, MyCarActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_shiming() {
        if (memberModel != null && memberModel.getPassengerauditstate() != null) {
            switch (memberModel.getPassengerauditstate()) {
                case "1":
                    goShiMing();
                    break;
                case "2":
                    shiMingWan();
                    break;
                case "3":
                    goShiMing();
                    break;
                case "4":
                    shiMingWan();
                    break;
            }
        }
    }

    private void goShiMing() {
        Intent intent = new Intent(this, ShiMingActivity_.class);
        startActivity(intent);
    }

    private void shiMingWan() {
        Intent intent = new Intent(this, SMRZTGActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_feedback() {
        Intent intent = new Intent(this, FeedBackActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_header() {
        Intent intent = new Intent(this, MyInformationActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_address() {
        Intent intent = new Intent(this, UsualAddressActivity_.class);
        startActivity(intent);
    }

    @Click
    void rl_message_center() {
        Routers.open(this, "gears_dccx://messagecenter");
    }

    @Click
    void re_dengji() {
        Intent intent = new Intent(this, RegisterRidingInformationActivity_.class);
        startActivity(intent);
    }

    @Click
    void re_about() {
        Intent intent = new Intent(this, AboutActivity_.class);
        startActivity(intent);
    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Click
    void btn_logout() {
        goLogout();
    }


    @Background
    void goLogout() {
        ResultModel<String> res = memberFinder.logout();
        showLogOutResult(res);
    }

    @UiThread
    void showLogOutResult(ResultModel<String> res) {
        Logger.d(res.getErrno() + "     " + res.getErrmsg());
        if (res != null) {
            if (res.getErrno().equals("0")) {
                sp.token().put("");
                sp.refreshToken().put("");
                sp.userId().put("");
                bus.post(new LogoutDriverMemberEvent());
//                app.removeUserAlias(memberModel.getId());
                app.deleteDriverTag();
                finish();
            } else {
                TastyToast.makeText(this, res.getErrmsg(), app.SHORT, app.WARNING);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCurrentMember();
    }

}
